import wx from 'weixin-js-sdk'
import API from '@/api/api.huaming.com.js'
export default function wxShare (shareTitle, shareDesc, shareLink, shareIcon) {
  let routerBase = process.env.ROUTER_BASE
  if (!shareTitle) shareTitle = '米饭干货铺'
  if (!shareDesc) shareDesc = '干货！满满的各种干货！确定不点进来搜刮一波吗？'
  if (!shareLink) shareLink = window.location.origin + routerBase
  if (!shareIcon) shareIcon = window.location.origin + routerBase + '/static/share-icon.png'

  setTimeout(() => {
    // 权限申请
    let authUrl = window.location.href
    console.error('授权 ', authUrl)
    API.getTicket({ 'url': authUrl }).then((res) => {
      wx.config({
        debug: false,
        appId: res.appId,
        timestamp: res.timestamp,
        nonceStr: res.nonceStr,
        signature: res.signature,
        jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline', 'chooseImage', 'uploadImage']
      })
    })
  }, 300)

  wx.ready(() => {
    // 分享至朋友
    wx.onMenuShareAppMessage({ title: shareTitle, desc: shareDesc, link: shareLink, imgUrl: shareIcon })
    // 分享至朋友圈
    wx.onMenuShareTimeline({ title: shareDesc, desc: shareDesc, link: shareLink, imgUrl: shareIcon })
  })
}
